package com.rpframework.website.friendly.act.admin;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.rpframework.utils.NumberUtils;
import com.rpframework.website.friendly.domain.Linkus;
import com.rpframework.website.friendly.service.LinkusService;

@Controller
@RequestMapping("/admin/linkus")
public class AdminLinkusAct extends AdminAct{
	@Resource LinkusService service;
	
	@RequestMapping("/edit-{id}")
	public String edit(@PathVariable Integer id, Map<Object, Object> model,RedirectAttributes attr){
		// get entity by service
		Linkus t = service.select(id);
		Assert.notNull(t,this.getClass().getName() + " Id 异常");
		// model put tablePager and this add redirect page replace modelpager
		model.put("pager", t);
		return this.add(attr, model);
	}
	@RequestMapping("/add")
	public String add(RedirectAttributes attr,Map<Object, Object> model){
		return this.doPackageURI("linkus/edit");
	}
	//this funtion is saveOrUpdate 
	@RequestMapping("/dosave")
	public String doSaveOrUpdate(@ModelAttribute Linkus t,
			RedirectAttributes attr){
		//service save or update
		boolean flag = service.doSaveOrUpdate(t);
		if(flag){
			if(NumberUtils.isValid(t.getId())) {//update
				setInfoMsg("更新操作成功！", attr);
			} else {
				setInfoMsg("新增操作成功！", attr);
			}
		}else{
			setInfoMsg("操作失败！", attr);
		}
		return redirect("/admin/linkus/edit-"+t.getId());
	}

}
